﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MuaChung.Models;
using MuaChung.Base;

namespace MuaChung.Controllers
{
    public class CouponController : Controller
    {
        //
        // GET: /Coupon/

        public ActionResult Index()
        {
            return RedirectToAction("ListAllCoupon");            
        }
        public ActionResult ListAllCoupon(int currentPage=1, int orderType = 1)
        {
            List<CouponDetailMin> model = new List<CouponDetailMin>();
            int pageSize = BaseWeb.PageSize;
            List<COUPON> tmplst = CouponModel.GetAll(currentPage,pageSize,orderType);
            ViewData["cp"] = currentPage;
            ViewData["ps"] = pageSize;
            ViewData["ot"] = orderType;
            if (tmplst == null)
            {
                ViewData["status"] = "NoItem";
                return View(model);
            }
            foreach (var item in tmplst)
            {
                CouponDetailMin tmp = CouponModel.ToCouponDetailMin(item);                
                model.Add(tmp);
            }
            ViewData["status"] = "OK";
            return View(model);
        }
        public ActionResult CouponDetail(int cid)
        {
            CouponDetail model = new CouponDetail();
            COUPON cp = CouponModel.FindByID(cid);
            CHITIET_COUPON ctcp = CouponModel.GetCouponDetail(cid);

            model.cid = cid;            
            model.doanhNghiep = cp.DOANHNGHIEP;
            model.giaTot = cp.Gia_Tot;
            model.maCoupon = cp.Ma_Coupon;
            model.giaGoc = cp.Gia_Goc;
            model.hinhAnh = cp.IMAGE_COUPON.ToList();
            //model.maLoai = cp.LOAI_COUPON.ID;            
            model.soLuongCanMua = cp.SoLuong_CanMua;
            model.soLuongCoupon = cp.SoLuong_Max;
            model.soLuongDatHang = cp.SoLuong_DaDat;
            model.tenSP = cp.Ten_SanPham;
            model.tgBatDau = cp.ThoiGian_BatDau;
            model.tgKetThuc = cp.ThoiGian_KetThuc;
            model.tyLeGiam = Convert.ToInt32(cp.TyLeGiam);
            //model.tenLoai = cp.LOAI_COUPON.TenLoai;
            try
            {
                model.dieuKien = ctcp.DieuKienSuDung;
                model.moTaTomTat = ctcp.MoTa_TomTat;
                model.moTa = ctcp.MoTa;
            }
            catch
            {
            }
            return View(model);
        }
        public ActionResult SearchCoupon()
        {
            return View();
        }
        public ActionResult SearchCouponResult(int currentPage = 1, int pageSize = 6, string keyword = "")
        {
            List<CouponDetailMin> model = new List<CouponDetailMin>();
            List<COUPON> tmplst = CouponModel.SearchCoupon(keyword, currentPage, pageSize);
            ViewData["cp"] = currentPage;
            ViewData["ps"] = pageSize;
            ViewData["key"] = keyword;
            foreach (var item in tmplst)
            {
                CouponDetailMin tmp = new CouponDetailMin();
                tmp.cid = item.ID;
                tmp.giaGoc = item.Gia_Goc;
                tmp.giaTot = item.Gia_Tot;
                tmp.maCoupon = item.Ma_Coupon;
                tmp.maLoai = Convert.ToInt32(item.Ma_Loai);
                //tmp.tenLoai = item.LOAI_COUPON.TenLoai;
                tmp.tgBatDau = item.ThoiGian_BatDau;
                tmp.tgKetThuc = item.ThoiGian_KetThuc;
                tmp.tyLeGiam = Convert.ToDouble(item.TyLeGiam);
                tmp.tenSP = item.Ten_SanPham;
                tmp.soLuongDatHang = item.SoLuong_DaDat;
                try
                {
                    tmp.hinhAnh = item.IMAGE_COUPON.First().URL;
                }
                catch
                {
                    tmp.hinhAnh = null;
                }
                if (item.SoLuong_DaDat >= item.SoLuong_Max)
                {
                    tmp.trangThai = "ChayHang";
                }
                else
                {
                    tmp.trangThai = "ConHang";
                }
                model.Add(tmp);
            }
            return View(model);
        }
        [HttpPost]
        public ActionResult SearchCouponResult(FormCollection form, int currentPage=1, int pageSize = 6)
        {                        
            string keyword = form.GetValues("searchKeyword").First();
            if (keyword == "")
            {
                ViewData["result"] = "NoKeyword";
                return View();
            }
            List<CouponDetailMin> model = new List<CouponDetailMin>();
            List<COUPON> tmplst = CouponModel.SearchCoupon(keyword,currentPage,pageSize);
            if (tmplst.Count <= 0)
            {
                ViewData["result"] = "NoItem";
            }
            ViewData["cp"] = currentPage;
            ViewData["ps"] = pageSize;
            ViewData["key"] = keyword;
            foreach (var item in tmplst)
            {
                CouponDetailMin tmp = new CouponDetailMin();
                tmp.cid = item.ID;
                tmp.giaGoc = item.Gia_Goc;
                tmp.giaTot = item.Gia_Tot;
                tmp.maCoupon = item.Ma_Coupon;
                tmp.maLoai = Convert.ToInt32(item.Ma_Loai);
                //tmp.tenLoai = item.LOAI_COUPON.TenLoai;
                tmp.tgBatDau = item.ThoiGian_BatDau;
                tmp.tgKetThuc = item.ThoiGian_KetThuc;
                tmp.tyLeGiam = Convert.ToDouble(item.TyLeGiam);
                tmp.tenSP = item.Ten_SanPham;
                tmp.soLuongDatHang = item.SoLuong_DaDat;
                try
                {
                    tmp.hinhAnh = item.IMAGE_COUPON.First().URL;
                }
                catch
                {
                    tmp.hinhAnh = null;
                }
                if (item.SoLuong_DaDat >= item.SoLuong_Max)
                {
                    tmp.trangThai = "ChayHang";
                }
                else
                {
                    tmp.trangThai = "ConHang";
                }
                model.Add(tmp);
            }
            return View(model);
        }
        public ActionResult ListNewCoupon(int numCoupon)
        {
            List<CouponDetailMin> model = new List<CouponDetailMin>();            
            List<COUPON> tmplst = CouponModel.GetNewCoupon(numCoupon);
            foreach (var item in tmplst)
            {
                CouponDetailMin tmp = new CouponDetailMin();
                tmp.cid = item.ID;
                tmp.giaGoc = item.Gia_Goc;
                tmp.giaTot = item.Gia_Tot;
                tmp.maCoupon = item.Ma_Coupon;
                tmp.maLoai = Convert.ToInt32(item.Ma_Loai);
                //tmp.tenLoai = item.LOAI_COUPON.TenLoai;
                tmp.tgBatDau = item.ThoiGian_BatDau;
                tmp.tgKetThuc = item.ThoiGian_KetThuc;
                tmp.tyLeGiam = Convert.ToDouble(item.TyLeGiam);
                tmp.tenSP = item.Ten_SanPham;
                tmp.soLuongDatHang = item.SoLuong_DaDat;
                tmp.serverLinkCoupon = "" + HttpContext.Request.Url;
                tmp.serverLinkCoupon = tmp.serverLinkCoupon.Remove(tmp.serverLinkCoupon.LastIndexOf('/'));
                tmp.serverLinkCoupon = tmp.serverLinkCoupon.Remove(tmp.serverLinkCoupon.LastIndexOf('/'));
                tmp.serverLinkCoupon += "/Coupon/CouponDetail?cid=" + tmp.cid;
                try
                {
                    string hinhAnh = item.IMAGE_COUPON.First().URL;
                    tmp.hinhAnh += hinhAnh;
                }
                catch
                {
                    tmp.hinhAnh = null;
                }
                if (item.SoLuong_DaDat >= item.SoLuong_Max)
                {
                    tmp.trangThai = "ChayHang";
                }
                else
                {
                    tmp.trangThai = "ConHang";
                }
                model.Add(tmp);
            }
            return View(model);
        }
        public ActionResult ListHotCoupon(int numCoupon)
        {
            List<CouponDetailMin> model = new List<CouponDetailMin>();
            List<COUPON> tmplst = CouponModel.GetHotCoupon(numCoupon);
            foreach (var item in tmplst)
            {
                CouponDetailMin tmp = new CouponDetailMin();
                tmp.cid = item.ID;
                tmp.giaGoc = item.Gia_Goc;
                tmp.giaTot = item.Gia_Tot;
                tmp.maCoupon = item.Ma_Coupon;
                tmp.maLoai = Convert.ToInt32(item.Ma_Loai);
                //tmp.tenLoai = item.LOAI_COUPON.TenLoai;
                tmp.tgBatDau = item.ThoiGian_BatDau;
                tmp.tgKetThuc = item.ThoiGian_KetThuc;
                tmp.tyLeGiam = Convert.ToDouble(item.TyLeGiam);
                tmp.tenSP = item.Ten_SanPham;
                tmp.soLuongDatHang = item.SoLuong_DaDat;
                tmp.serverLinkCoupon = "" + HttpContext.Request.Url;
                tmp.serverLinkCoupon = tmp.serverLinkCoupon.Remove(tmp.serverLinkCoupon.LastIndexOf('/'));
                tmp.serverLinkCoupon = tmp.serverLinkCoupon.Remove(tmp.serverLinkCoupon.LastIndexOf('/'));
                tmp.serverLinkCoupon += "/Coupon/CouponDetail?cid=" + tmp.cid;
                try
                {
                    string hinhAnh = item.IMAGE_COUPON.First().URL;
                    tmp.hinhAnh = "" + HttpContext.Request.Url;
                    tmp.hinhAnh = tmp.hinhAnh.Remove(tmp.hinhAnh.LastIndexOf('/'));
                    tmp.hinhAnh = tmp.hinhAnh.Remove(tmp.hinhAnh.LastIndexOf('/'));
                    tmp.hinhAnh += hinhAnh;
                }
                catch
                {
                    tmp.hinhAnh = null;
                }
                if (item.SoLuong_DaDat >= item.SoLuong_Max)
                {
                    tmp.trangThai = "ChayHang";
                }
                else
                {
                    tmp.trangThai = "ConHang";
                }
                model.Add(tmp);
            }
            return View(model);
        }
               
        public ActionResult OrderDialog(string maCoupon, int cid)
        {
             ViewData["MaCoupon"] = maCoupon;
             ViewData["Cid"] = cid;
             return View();            
        }      

        [Authorize]
        public ActionResult Order(FormCollection frm)
        {
            if (CouponModel.Cart.lstOrder == null)
            {
                CouponModel.InitCart(User.Identity.Name);
            }
            if (frm.AllKeys[0] == "Order")
            {
                if (CouponModel.OrderCouponsInCart())
                {
                    ViewData["Message"] = "Đặt hàng thành công.";
                }
                else
                {
                    ViewData["Message"] = "Đặt hàng thất bại";
                }
                return View(CouponModel.Cart);
            }
            else
            {
                string maCoupon = frm.GetValue("maCoupon").AttemptedValue;
                int soluong = Convert.ToInt32(frm.GetValue("soLuong").AttemptedValue);
                foreach (var item in CouponModel.Cart.lstOrder)
                {
                    if (item.COUPON.Ma_Coupon == maCoupon)
                    {
                        item.SoLuong += soluong;
                        item.TongGia = item.Gia * item.SoLuong;
                        item.NgayDatHang = DateTime.Now;
                        return View(CouponModel.Cart);
                    }
                }
                DONHANG tmp = new DONHANG();
                COUPON cp = CouponModel.FindByMaCoupon(maCoupon);
                tmp.Ma_Coupon = cp.ID;
                tmp.NgayDatHang = DateTime.Now;
                tmp.Gia = cp.Gia_Tot;
                tmp.COUPON = new COUPON();
                tmp.COUPON = cp;
                tmp.SoLuong = soluong;
                tmp.TongGia = tmp.Gia * tmp.SoLuong;
                tmp.Ma_TK = CouponModel.Cart.Ma_TK;
                tmp.TrangThai = 0;
                CouponModel.Cart.lstOrder.Add(tmp);
            }
            return View(CouponModel.Cart);
        }
        [Authorize]
        public ActionResult WUC_MyCart()
        {
            return View(CouponModel.Cart);
        }      
    }
}
